package com.xiaolei.baselib.base;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import com.xiaolei.baselib.annotation.AssetOpenInputStream;
import com.xiaolei.baselib.annotation.AutoWrite;
import com.xiaolei.baselib.annotation.Editor;
import com.xiaolei.baselib.annotation.SharedPreference;

/**
 * Created by xiaolei on 16/5/14.
 */
public abstract class BaseApplication extends Application
{
    private Field fields[] = null;
    private AssetManager mAssetManager;
    @Override
    public void onCreate()
    {
        super.onCreate();
        try
        {
            initAnotationViews();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (InstantiationException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        init();
    }
    private void initAnotationViews() throws IllegalAccessException, InstantiationException, IOException
    {
        fields = this.getClass().getDeclaredFields();

        for (int i = 0;i<fields.length;i++)
        {
            Field field = fields[i];
            //java.lang.IllegalAccessException: access to field not allowed
            if(field.isAnnotationPresent(SharedPreference.class))//对SharedPreference进行注入
            {
                field.setAccessible(true);//设置私有的  可访问属性 为true
                SharedPreference findid = field.getAnnotation(SharedPreference.class);
                String name = findid.value();
                SharedPreferences sharedPreferences = this.getSharedPreferences(name, MODE_PRIVATE);
                field.set(this,sharedPreferences);
            }else if(field.isAnnotationPresent(Editor.class))//对SharedPreference.Editor进行注入
            {
                field.setAccessible(true);//设置私有的  可访问属性 为true
                Editor editor = field.getAnnotation(Editor.class);
                String name = editor.value();
                SharedPreferences sharedPreferences = this.getSharedPreferences(name, MODE_PRIVATE);
                SharedPreferences.Editor d = sharedPreferences.edit();
                field.set(this,d);
            }else if(field.isAnnotationPresent(AutoWrite.class))//是普通的注解
            {
                field.setAccessible(true);//设置私有的  可访问属性 为true
                AutoWrite autowrite = field.getAnnotation(AutoWrite.class);
                Class clazz = autowrite.value();
                if(clazz == Void.class)
                {
                    clazz = field.getType();
                }
                field.set(this,clazz.newInstance());
            }else if(field.isAnnotationPresent(AssetOpenInputStream.class))//是打开资源文件夹的注解
            {
                if(field.getType() == InputStream.class)
                {
                    field.setAccessible(true);//设置私有的  可访问属性 为true
                    AssetOpenInputStream aso = field.getAnnotation(AssetOpenInputStream.class);
                    String name = aso.value();
                    if(mAssetManager == null)
                    {
                        mAssetManager = getAssets();
                    }
                    InputStream inputStream = mAssetManager.open(name);
                    field.set(this,inputStream);
                }
            }
        }
    }
    public abstract void init();
    private Toast toast = null;

    protected void Alert(String msg)
    {
        if (toast == null)
        {
            toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }
}
